#include <memory>
#include "Protocol.hpp"
#include "Tool.hpp"
#include "InetAddr.hpp"
#include "Socket.hpp"

void GetScanf(int &x, int &y, char &oper)
{
    std::cout << "please Enter x:";
    std::cin >> x;
    std::cout << "please Enter y:";
    std::cin >> y;
    std::cout << "please Enter oper:";
    std::cin >> oper;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cout << "please ./TcpClient ip port" << std::endl;
        exit(USAGE_ERR);
    }

    uint16_t port = std::stoi(argv[2]);
    std::string ip = argv[1];

    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->BuildTcpSocketClient();
    // 建立连接
    if (client->Connect(ip, port) != 0)
    {
        std::cout << "连接失败" << std::endl;
        exit(CONNECT_ERR);
    }

    std::unique_ptr<Protocal> procal = std::make_unique<Protocal>();
    std::string buff_str;
    while (true)
    {
        int x, y;
        char oper;
        GetScanf(x, y, oper);
        // 转为序列化
        std::string send_str = procal->BuidSendStr(x, y, oper);
        // 发送请求
        client->Send(send_str);

        // 接收应答
        Response rep(0,0);
        bool ok = procal->GetResponse(client, buff_str, &rep);
        if(!ok)
        break;
        //拿到结果
        std::cout<<"结果为"<<rep.GetResult()<<"["<<rep.GetCode()<<"]"<<std::endl;
    }
    client->Close();
    return 0;
}